package com.administrator.customviewtest.view_group;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

/**
 * Created by Administrator on 2017/5/2.
 */

public class TestViewGroup7 extends ViewGroup {
    View childView;
    int yDown = 0;
    int currentY = 0;
    int yMove = 0;
    private boolean isIntercept;

    public TestViewGroup7(Context context) {
        this(context, null);
    }

    public TestViewGroup7(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestViewGroup7(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {

    }

    private static final String TAG = "TestViewGroup7";

    @Override
    public boolean onInterceptHoverEvent(MotionEvent event) {
        return super.onInterceptHoverEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_CANCEL:
                Log.e(TAG, "ACTION_CANCEL");
                break;
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "DOWN");
                yDown = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e(TAG, "MOVE");
                currentY = (int) event.getY();
                yMove = currentY - yDown;
                if (yMove > 0)
                    childView.layout(childView.getLeft(), yMove, childView.getRight(), childView.getHeight() + yMove);
                Log.e("move=", yMove + "");
                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, "UP");
                childView.layout(childView.getLeft(), 0, childView.getRight(), childView.getHeight());
                break;
        }
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
//        childView = getChildAt(0);
//        Log.e(TAG,childView.getMeasuredWidth()+" "+childView.getMeasuredHeight());
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        int y = (int) event.getY();


        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                yDown = y;
                break;
            case MotionEvent.ACTION_MOVE:
                yMove = y;
                if (yMove - yDown < 0) {
                    isIntercept = false;
                } else if (yMove - yDown > 0) {
                    isIntercept = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return isIntercept;
    }

    @Override
    protected void onLayout(boolean b, int left, int top, int right, int bottom) {

        childView = getChildAt(0);
//        Log.e(TAG,childView.getBottom()+" "+childView.getHeight()+" "+childView.getMeasuredHeight());



        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());


    }
}
